/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roles;

import avenue.MyWindowEvent;
import avenue.Utils;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import model.RolePermission;


public class EditRolePermission extends javax.swing.JFrame {

    /**
     * Creates new form EditRolePermission
     */
    public EditRolePermission() {
        initComponents();

        Utils.centerOnScreen(this);
        Utils.logUCstart(this);
    }

    public void addChoice(String s) {
        choice1.add(s);
    }

    public EditRolePermission(RolePermission rp) {
        initComponents();
        Utils.logUCstart(this);
        rollPermission = rp;

        jLbRoleName.setText(rollPermission.getRoleId().getRoleName());
        jChkCreate.setSelected(Utils.intToBool(rollPermission.getCanCreate()));
        jChkRetreive.setSelected(Utils.intToBool(rollPermission.getCanRetrieve()));
        jChkUpdate.setSelected(Utils.intToBool(rollPermission.getCanUpdate()));
        jChkDelete.setSelected(Utils.intToBool(rollPermission.getCanDelete()));
        jChkStationLevelAccess.setSelected(Utils.intToBool(rollPermission.getStationLevelAccess()));

        Utils.centerOnScreen(this);
    }

    private void closeMe(boolean exitAndSave) {
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, exitAndSave);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
        }
        this.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        choice1 = new java.awt.Choice();
        jLabel2 = new javax.swing.JLabel();
        jChkCreate = new javax.swing.JCheckBox();
        jChkRetreive = new javax.swing.JCheckBox();
        jChkUpdate = new javax.swing.JCheckBox();
        jChkDelete = new javax.swing.JCheckBox();
        jChkStationLevelAccess = new javax.swing.JCheckBox();
        jLbRoleName = new javax.swing.JLabel();
        jBtnCancel = new javax.swing.JButton();
        jBtnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Διαχείριση Δικαιώματος");
        setResizable(false);

        jLabel1.setText("Όνομα Ρόλου : ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Module");

        jChkCreate.setSelected(true);
        jChkCreate.setText("Εισαγωγή");

        jChkRetreive.setSelected(true);
        jChkRetreive.setText("Αναζήτηση");

        jChkUpdate.setSelected(true);
        jChkUpdate.setText("Επεξεργασία");

        jChkDelete.setSelected(true);
        jChkDelete.setText("Διαγραφή");

        jChkStationLevelAccess.setSelected(true);
        jChkStationLevelAccess.setText("Ορατότητα σταθμού");

        jLbRoleName.setText("jLabel3");

        jBtnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        jBtnCancel.setText("Ακυρο");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });

        jBtnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        jBtnSave.setText("Αποθήκευση");
        jBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(choice1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jChkCreate)
                            .addComponent(jChkRetreive)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLbRoleName))
                            .addComponent(jLabel2)
                            .addComponent(jChkUpdate)
                            .addComponent(jChkDelete)
                            .addComponent(jChkStationLevelAccess)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jBtnCancel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnSave)))
                        .addGap(0, 86, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLbRoleName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(2, 2, 2)
                .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkRetreive)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkCreate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkStationLevelAccess)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSave)
                    .addComponent(jBtnCancel))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        // TODO add your handling code here:
        closeMe(false);
    }//GEN-LAST:event_jBtnCancelActionPerformed

    private void jBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSaveActionPerformed
        rollPermission.setModuleId(Utils.moduleByDescription(choice1.getSelectedItem()));

        rollPermission.setCanCreate(Utils.boolToInt(jChkCreate.isSelected()));
        rollPermission.setCanUpdate(Utils.boolToInt(jChkUpdate.isSelected()));

        rollPermission.setCanDelete(Utils.boolToInt(jChkDelete.isSelected()));
        rollPermission.setCanRetrieve(Utils.boolToInt(jChkRetreive.isSelected()));

        rollPermission.setStationLevelAccess(Utils.boolToInt(jChkStationLevelAccess.isSelected()));


        closeMe(true);
    }//GEN-LAST:event_jBtnSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditRolePermission.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditRolePermission.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditRolePermission.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditRolePermission.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditRolePermission().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Choice choice1;
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnSave;
    private javax.swing.JCheckBox jChkCreate;
    private javax.swing.JCheckBox jChkDelete;
    private javax.swing.JCheckBox jChkRetreive;
    private javax.swing.JCheckBox jChkStationLevelAccess;
    private javax.swing.JCheckBox jChkUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLbRoleName;
    // End of variables declaration//GEN-END:variables
    EntityManager em;
    RolePermission rollPermission;
}
